package com.upb.webserver.applicationorg;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.enums.PlatformEnum;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.tools.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;

@Log4j2
@RestController
@RequestMapping(value = "application-org")
public class ApplicationOrgController {
    @Autowired
    private ApplicationOrgService applicationOrgService;
    @Autowired
    private AccountService accountService;


    @GetMapping(value = "/list")
    public BasePageResponse queryApplicationOrgByPage(@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                                      @RequestParam(value = "pageSize", required = false, defaultValue = "50") Integer pageSize,
                                                      @RequestParam(value = "orgType", required = false) Byte orgType,
                                                      @RequestParam(value = "applicationId") Integer applicationId) {
        log.info("start exec method [queryApplicationOrgByPage]. pageNumber:{} pageSize:{} orgType:{} applicationId:{}", pageNumber, pageSize, orgType, applicationId);
        BasePageResponse response = applicationOrgService.queryApplicationOrgByPage(pageNumber, pageSize, orgType, applicationId);
        log.info("success exec method [queryApplicationOrgByPage]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param applicationId
     * @param request
     * @return
     */
    @GetMapping(value = "/{applicationId}/selOrg")
    public BaseResponse getSelOrgByApplication(@PathVariable("applicationId") Integer applicationId, HttpServletRequest request) {
        log.info("start exec method [getSelOrgByApplication]. applicationId:{}", applicationId);
        BaseResponse baseResponse = BaseResponse.initSuccess();
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        if (PlatformEnum.DEVELOPER.getId() == accountCommon.getPlatform()) {
            Set<Integer> allOrgId = applicationOrgService.listOrgIdByApplicationAndDeveloper(applicationId, accountCommon.getId());
            baseResponse.setData(allOrgId);
        }
        log.info("finish exec method [getSelOrgByApplication]. applicationId:{} result:{}", applicationId, JsonUtils.objToString(baseResponse));
        return baseResponse;
    }


//    @GetMapping(value = "/getOrgByAppAndOrder")
//    public BaseResponse getOrgByAppAndOrder(@RequestParam(value = "applicationId") Integer applicationId, @RequestParam(value = "orderId") Integer orderId, HttpServletRequest request) {
//        log.info("start exec method [getOrgByAppAndOrder]. applicationId:{} orderId:{}", applicationId, orderId);
//        AccountCommon accountCommon = accountService.getAccountCommon(request);
//        BaseResponse baseResponse = crossChainService.getOrgByAppAndOrder(applicationId, orderId, accountCommon);
//        log.info("finish exec method [getOrgByAppAndOrder]. applicationId:{} orderId:{} result:{}", applicationId, orderId, JsonUtils.objToString(baseResponse));
//        return baseResponse;
//    }
}